package ms.unicorn.diagram.sheet;

import ms.unicorn.Element;
import ms.unicorn.diagram.part.UnicornVisualIDRegistry;
import ms.unicorn.diagram.providers.UnicornElementTypes;
import ms.unicorn.dsl.ILanguageElement;
import ms.unicorn.dsl.LanguageRegistry;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.graphics.Image;

/**
 * @generated
 */
public class UnicornSheetLabelProvider extends BaseLabelProvider implements
		ILabelProvider {

	/**
	 * @generated NOT
	 */
	public String getText(Object element) {
		if (element instanceof IStructuredSelection) {
			IStructuredSelection sel = (IStructuredSelection) element;
			Object selObj = sel.getFirstElement();
			if (selObj instanceof EditPart) {
				EditPart edpart = (EditPart) selObj;
				if (edpart.getModel() instanceof View) {
					View v = (View) edpart.getModel();
					EObject e = v.getElement();
					if (e instanceof Element) {
						ILanguageElement le = LanguageRegistry.INSTANCE.getLanguageElement((Element) e);
						if (le != null)
							return le.getText(); 
					}
				}
			}	
		}
		element = unwrap(element);
		IElementType etype = getElementType(getView(element));
		return etype == null ? "" : etype.getDisplayName();
	}

	/**
	 * @generated NOT
	 */
	public Image getImage(Object element) {
		if (element instanceof IStructuredSelection) {
			IStructuredSelection sel = (IStructuredSelection) element;
			Object selObj = sel.getFirstElement();
			if (selObj instanceof EditPart) {
				EditPart edpart = (EditPart) selObj;
				if (edpart.getModel() instanceof View) {
					View v = (View) edpart.getModel();
					EObject e = v.getElement();
					if (e instanceof Element) {
						ILanguageElement le = LanguageRegistry.INSTANCE.getLanguageElement((Element) e);
						if (le != null && le.getSmallImage() != null)
							return le.getSmallImage().createImage();
					}
				}
			}	
		}
		IElementType etype = getElementType(getView(unwrap(element)));
		return etype == null ? null : UnicornElementTypes.getImage(etype);
	}

	/**
	 * @generated
	 */
	private Object unwrap(Object element) {
		if (element instanceof IStructuredSelection) {
			return ((IStructuredSelection) element).getFirstElement();
		}
		return element;
	}

	/**
	 * @generated
	 */
	private View getView(Object element) {
		if (element instanceof View) {
			return (View) element;
		}
		if (element instanceof IAdaptable) {
			return (View) ((IAdaptable) element).getAdapter(View.class);
		}
		return null;
	}

	/**
	 * @generated
	 */
	private IElementType getElementType(View view) {
		// For intermediate views climb up the containment hierarchy to find the one associated with an element type.
		while (view != null) {
			int vid = UnicornVisualIDRegistry.getVisualID(view);
			IElementType etype = UnicornElementTypes.getElementType(vid);
			if (etype != null) {
				return etype;
			}
			view = view.eContainer() instanceof View ? (View) view.eContainer()
					: null;
		}
		return null;
	}

}
